﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ServerPlatform.Web;
using MapEntities;

namespace WebApp.Controls
{
    public partial class LayerItemBehaviourEditor : BaseDataEditor<LayerItemBehaviour>
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                foreach (string xlevel in Enum.GetNames(typeof(TransparencyLevel)))
                {
                    ctlTransparencyLevel.Items.Add(new ListItem(xlevel, xlevel));
                }
            }
        }
        public override void ClearEditor()
        {
            ctlTransparencyLevel.SelectedIndex = 0;
            ctlRedirectAddressDouble.Text = string.Empty;
            ctlRedirectAddressLeft.Text = string.Empty;
            ctlRedirectAddressRight.Text = string.Empty;
            ctlZoomLevelDouble.Text = string.Empty;
            ctlZoomLevelLeft.Text = string.Empty;
            ctlZoomLevelRight.Text = string.Empty;
        }

        protected override void ShowEntity(LayerItemBehaviour entity)
        {
            ctlTransparencyLevel.SelectedIndex = ctlTransparencyLevel.Items.IndexOf(ctlTransparencyLevel.Items.FindByText(entity.ItemTransparencyLevel.ToString()));
            ctlRedirectAddressDouble.Text = entity.DoubleMouseClick.RedirectAddress;
            ctlZoomLevelDouble.Text = !entity.DoubleMouseClick.ZoomToLevel.HasValue ? 0.ToString() : entity.DoubleMouseClick.ZoomToLevel.ToString();
            ctlRedirectAddressLeft.Text = entity.LeftMouseClick.RedirectAddress;
            ctlZoomLevelLeft.Text = !entity.LeftMouseClick.ZoomToLevel.HasValue ? 0.ToString() : entity.LeftMouseClick.ZoomToLevel.ToString();
            ctlRedirectAddressRight.Text = entity.RightMouseClick.RedirectAddress;
            ctlZoomLevelRight.Text = !entity.RightMouseClick.ZoomToLevel.HasValue ? 0.ToString() : entity.RightMouseClick.ZoomToLevel.ToString();

        }

        public override LayerItemBehaviour GetEntity()
        {
            Current.DoubleMouseClick.RedirectAddress = ctlRedirectAddressDouble.Text.Trim();
            Current.LeftMouseClick.RedirectAddress = ctlRedirectAddressLeft.Text.Trim();
            Current.RightMouseClick.RedirectAddress = ctlRedirectAddressRight.Text.Trim();
            Current.ItemTransparencyLevel = (TransparencyLevel)Enum.Parse(typeof(TransparencyLevel), ctlTransparencyLevel.SelectedItem.Text);                
            int zoomdouble, zoomright, zoomleft;
            if (int.TryParse(ctlZoomLevelDouble.Text, out zoomdouble) && zoomdouble != 0) Current.DoubleMouseClick.ZoomToLevel = zoomdouble; else Current.DoubleMouseClick.ZoomToLevel = null;
            if (int.TryParse(ctlZoomLevelRight.Text, out zoomright) && zoomright != 0) Current.RightMouseClick.ZoomToLevel = zoomright; else Current.RightMouseClick.ZoomToLevel = null;
            if (int.TryParse(ctlZoomLevelLeft.Text, out zoomleft) && zoomleft != 0) Current.LeftMouseClick.ZoomToLevel = zoomleft; else Current.LeftMouseClick.ZoomToLevel = null;
            ClearEditor();
            return Current;
            

        }
    }
}